package com.example.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * @author
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionAdvice {


    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseModel<Object> runtimeExceptionHandler(RuntimeException ex) {
        log.info("runtimeExceptionHandler errorHandler....");
        log.error(ex.getMessage(), ex);
        return new ResponseModel<>("00000001", "系统运行时错误");
    }

    @ResponseBody
    @ExceptionHandler(value = BizException.class)
    public ResponseModel<Object> bizExceptionHandler(BizException ex) {
        log.info("runtimeExceptionHandler errorHandler....");
        log.error(ex.getMessage(), ex);
        return new ResponseModel<>("00000001", ex.getMessage());
    }

    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public ResponseModel<Object> httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException ex) {
    	log.info("HttpRequestMethodNotSupportedException....");
        log.error(ex.getMessage());
        return new ResponseModel<>("00000001", ex.getMessage());
    }
    
    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseModel<Object> exceptionHandler(Exception ex) {
        log.info("errorHandler exception....");
        log.error(ex.getMessage(), ex);
        return new ResponseModel<>("00000001", "系统错误");
    }

    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Throwable.class)
    public ResponseModel<Object> errorHandler(Throwable ex) {
        log.info("exceptionHandler Throwable....");
        log.error(ex.getMessage(), ex);
        return new ResponseModel<>("00000001", "系统错误");
    }
}
